﻿(function(){
	
	com.yogi.core.FlexPart = function(config) {
		/**
		 * 事件号：零件显示/消失 结束，这要在Tween完以后发出
		 */
		com.yogi.core.FlexPart.EVENT_SHOW_DONE = "EVENT_FLEXPART_SHOW_DONE";
		com.yogi.core.FlexPart.DIV_ID_PREFIX = "flexpart_";
		
		var m_config = config;
		var m_div = $("<div id=\"" + com.yogi.core.FlexPart.DIV_ID_PREFIX + config.Id + "\" style=\"position:absolute;overflow:hidden;\"></div>");
		var m_div_design = $("<div style=\"position:absolute;overflow:hidden;border:2px dashed blue\"></div>");
		var m_component;
		var m_hype_doc = null;
		var m_html_doc = null;
		var m_resLoaded = false;
		var m_res_width;
		var m_res_height;
		//显示结束的信号是否已发送。
		//由于可能存在多个显示模式处理器，如果其中一个处理器已发出显示结束的信号，则禁止再发送信号
		var m_display_done_event_sent = false;
		var m_status_show = false;
		
		var m_ref_part = null;
		var m_displayParams;
		var m_pos;
		
		var m_doc_guid = false;  //for cache
		
		var m_wrapperManager = window.contentPanel.getWrapperManager();
		
		function doGetElement(elem_show_name) {
			var elem_id = doGetElementId(elem_show_name);			
			if(!Ext.isEmpty(elem_id)) {
				var elem = $("#" + elem_id);
				return elem;
			}
			return null;
		}
		function doGetElementId(elem_show_name){
			if(m_hype_doc){
				var elem_guid = m_hype_doc.elements_map[elem_show_name];
				return elem_guid;
			} else {
				if(!m_doc_guid) {
					m_doc_guid = getDocGuid();					
				}
				if(!Ext.isEmpty(m_doc_guid)){
					return elem_show_name + "_" + m_doc_guid;
				}
			}
			return "";
		}
		/**
		 * 通知外部显示的过程结束了
		 */
		function notifyDisplayDone() {
			if(!m_display_done_event_sent){
				m_display_done_event_sent = true;
				m_div.trigger( com.yogi.core.FlexPart.EVENT_SHOW_DONE );
			}
		}
		function doShow(bShow){
			m_display_done_event_sent = false;				//重置此值，否则不能发出结束信号了
			if(m_status_show){
				//现在已经是呈现状态
				if(bShow) {
					notifyDisplayDone();
					return;
				}
			}else{
				if(!bShow) {	//想要隐藏,而现在已经是隐藏状态
					notifyDisplayDone();
					return;
				}
			}
			m_status_show = bShow;		//更新状态
			if(bShow){
				m_div.fadeIn(350, function(){
					if($.browser.msie){
						if(parseFloat($.browser.version) < 7.0){
							//DD_belatedPNG.fixPng(document.getElementById("ContentPanel"));
							//_self.getDIV().pngFix();
							//$(document).pngFix();
						}
					}
					notifyDisplayDone();
				});
				if(window.contentPanel.designSite) {
					try{
						m_hype_doc.showSceneNamed(m_hype_doc.currentSceneName());
					}catch(e){  }
				}
			}else{
				m_div.fadeOut(350, function(){
					notifyDisplayDone();
				});
			}
		}
		function processPosition() {
			if(! Ext.isEmpty(m_config.Position)) {		//位置			
				m_pos = $.parseJSON(m_config.Position);
			}
			if(!m_pos){
				m_pos = {x:0,y:0};
			}
			if(m_pos){		//位置	
				if(m_pos.x != null && m_pos.y != null){		//有坐标就直接使用坐标							
					m_div.css('left',m_pos.x);
					m_div.css('top',m_pos.y);
				}else{
					processAlign(null);
					$(window).bind('resize', function() { 
						processAlign(null);
					});
				}
			}
		}
		function processAlign() {
			var align = m_pos.align;
			var offset = m_pos.offset;
			var x = 0;
			var y = 0;
			var ctWidth;
			var ctHeight;
			ctWidth = $(window).width();
			ctHeight = $(window).height();
			
			if(align[0] == "left"){
				x = 0;
			}else if(align[0] == "right"){
				x = ctWidth - m_res_width;
			}else if(align[0] == "center"){
				x = (ctWidth - m_res_width) / 2;
			}
			if(align[1] == "top"){
				y = 0;
			}else if(align[1] == "bottom"){
				y = ctHeight - m_res_height;
			}else if(align[1] == "center"){
				y = (ctHeight - m_res_height) / 2;
			}
			if(offset != null){
				var offset_x = parseFloat(offset[0]);
				var offset_y = parseFloat(offset[1]);
				if(align[0] == "left") {
					x += offset_x;
				} else if(align[0] == "right"){
					x -= offset_x;
				}
				if(align[1] == "top") {
					y += offset_y;
				} else if(align[1] == "bottom"){
					y -= offset_y;
				}				
			}
			m_div.css('left',x);
			m_div.css('top',y);
		}
		function getDocGuid() {
			var comp_id = m_component.attr("id");			
			if(comp_id && comp_id.indexOf(com.yogi.core.JsLoader.PRELOAD_DIV_PREFIX) == 0){		//hype doc
				var doc_guid = comp_id.substr(com.yogi.core.JsLoader.PRELOAD_DIV_PREFIX.length);
				return doc_guid;
			}
			return "";
		}
		function analyzeDocElements(ref_part) {
			if(ref_part) {
				m_hype_doc = ref_part.getHypeDoc();
				m_html_doc = ref_part.getHtmlDoc();
			} else {
				var doc_guid = getDocGuid();
				if(!Ext.isEmpty(doc_guid)){
					m_hype_doc = HYPE.documents[doc_guid];
					m_html_doc = com.yogi.core.ContentPanel.html_documents[doc_guid];
				}
			}			
		}
		function doInitUI(res_div, ref_part) {
			m_resLoaded = true;
			m_component = res_div;
			analyzeDocElements(ref_part);
			
//			if(ref_part != null && ref_part.getDIV() == res_div){
//				m_res_width = ref_part.res_width;
//				m_res_height = ref_part.res_height;
//			}else{
//				m_res_width = parseInt(res_div.css("width"));
//				m_res_height = parseInt(res_div.css("height"));
//			}			
			m_res_width = parseInt(res_div.css("width"));
			m_res_height = parseInt(res_div.css("height"));
			
			if(m_res_width > 0 && m_res_height > 0){
				m_div.css("width", m_res_width + "px");
				m_div.css("height", m_res_height + "px");
			}
			if(!ref_part){						//如果是复合控件，纯粹为了操纵子控件，不需要添加到显示列表，但是要记录复合控件的引用，因为对于 元件自身多个包装器的情况，某些动作如 mouseover 必须引用实际的Part				
				res_div
					.appendTo(m_div);	
			}else{
				m_ref_part = ref_part;
			}						
			//Ext.dump('processPosition.' + m_config.Id);
			//处理位置问题
			processPosition();
					
			//处理包装器
			processWrapper();
			processDisplayParams();			
		}
		function processDisplayParams() {
			if(!Ext.isEmpty( m_config.DisplayParams )) {
				var configValue = $.parseJSON(m_config.DisplayParams);		//可以是多种方式的混合
				if(Ext.isArray( configValue)) {
					m_displayParams = configValue;
				}else{
					m_displayParams = [configValue];
				}
			}
		}
		function processWrapper() {
			if(m_config.WrapperParam != null ){
				var wrapperConfig;
				if(Ext.isString( m_config.WrapperParam) ){
					if(!Ext.isEmpty( config.WrapperParam)) {
						wrapperConfig =  $.parseJSON(config.WrapperParam);
					}else{
						wrapperConfig = null;
					}
				}else{
					wrapperConfig = config.WrapperParam;
				}
				if(wrapperConfig != null){
					//Ext.dump(m_config.Id + ":" + wrapperConfig.type);
					m_wrapper = m_wrapperManager.getWrapperInstance(wrapperConfig.type);
					m_config.ref_part = m_ref_part;		//reference back
					if(m_wrapper != null) {
						m_wrapper.wrap(_self, wrapperConfig, m_config);	
					}
				}
			}
		}
		function doHighLight(off) {			
			if(off){
				m_div_design.remove();
			}else{
				m_div_design
					.css("zIndex",m_div.css("zIndex") + 1)
					.css("width",m_div.css("width"))
					.css("height",m_div.css("height"))
					.css("left",m_div.css("left"))
					.css("top",m_div.css("top"))
					.appendTo(m_div.parent())	
					.draggable({ 
						cursor: 'crosshair',
						delay: 200,
						start: function() {
							
						},
						drag: function(event, ui) {
							var x = m_div_design.css("left");
							var y = m_div_design.css("top");
							m_div.css("left", x);
							m_div.css("top", y);
							window.contentPanel.designSite.onPartDragging({ x:parseInt(x), y:parseInt(y)	});
						}
					});
			}
		}
		var _self = {
			objType:"FlexPart interface",
			resetDisplayParam: function () {
				m_displayParams = null;
			},
			waitForDiv: function(callback) {	
				var retry_times = 10;
				var wait_func = function() {
					var result = _self.getDIV();
					if(result.length <= 0) {
						result = $(result.selector);
					}
					if(result.length > 0){
						if(m_ref_part) {
							m_component = result;
						}else{
							m_div = result;
						}
						callback(result);
					}else{					
						if(retry_times-- > 0){
							wait_func.defer(300);
						} else {
							if(typeof console != 'undefined'){
								console.log("no div for flexpart." + config.Id);
							}
						}
					}
				};
				wait_func.defer(100);
			},
			getDIV: function() {
				if(m_ref_part) {					
					return m_component;		//return the inner element directly
				}
				return m_div;
			},
			getElement: function(show_name) {
				return doGetElement(show_name);
			},
			getElementId: function(show_name) {
				return doGetElementId(show_name);
			},
			getHypeDoc: function(){
				return m_hype_doc;
			},
			getHtmlDoc: function() {
				return m_html_doc;
			},
			getConfig: function() {
				return m_config;
			},
			initUI: function(res_div, ref_part) {
				doInitUI(res_div, ref_part);				
			},
			show: function(bShow) {
				doShow(bShow);
			},
			highLight: function(off) {
				doHighLight(off);
			},
			test_func: function(func_name, args){
				eval("var inner_func = " + func_name);
				if($.isFunction(inner_func)){
					return inner_func.apply(this, args);
				}
				return inner_func;
			},
			test_setter: function(lh,rh) {
				var stmt = lh + "=" + rh + ";";
				eval(stmt);
			}	
		};
		return _self;
	};
	com.yogi.core.FlexPartDisplay = function(page){
		com.yogi.core.FlexPartDisplay.EVENT_DONE = "flexpart_display_all_done";
		
		var m_relate_page = page;
		var m_contentpanel = window.contentPanel;
		//需要显示/消隐的零件总数
		var m_part_count_remain = 0;
		
		function onPartShowDone(e) {
			m_part_count_remain--;
			if(m_part_count_remain == 0) {		
				$(self).trigger(com.yogi.core.FlexPartDisplay.EVENT_DONE, { page: m_relate_page } );		//处理完毕
			}
		}
		var self = {
			/**
			 * 统一显示/消隐零件
			 * @param parts_config 零件配置信息数组
			 * @param show 显示还是消隐
			 */
			showParts: function (parts_config,bShow) {
				m_part_count_remain = parts_config.length;
				for(var i=0;i < parts_config.length; i++) {		
					var part = m_contentpanel.getPart(parts_config[i]["Id"]);					
					part.getDIV().bind(com.yogi.core.FlexPart.EVENT_SHOW_DONE,onPartShowDone);
					part.show(bShow);
				}
				return true;
			},
			getEventPump:function(){
				return event_pump;
			}
		};
		return self;
	};
	function dummy(){
		new com.yogi.core.FlexPart();
	}
	$(window).trigger(com.yogi.core.EVENT_EXTERNAL_SCRIPT_LOADED,  { script:"com.yogi.core.FlexPart" });
})();	